#coding=utf-8

import re,urllib

sh_code_url = 'http://biz.finance.sina.com.cn/stock/market_hq.php?type=61&by=symbol&order=asc'
sz_code_url = 'http://biz.finance.sina.com.cn/stock/market_hq.php?type=63&by=symbol&order=asc'

def get_code(market='sh'):
    
    sh_code = re.compile( r'sh\d{6}')
    sz_code = re.compile( r'sz\d{6}')
    
    if market == 'sh':
        html = urllib.urlopen(sh_code_url).read()
        codes = sorted( list( set( sh_code.findall(html) ) ) )
        
    else:
        html = urllib.urlopen(sz_code_url).read()
        codes = sorted( list( set( sz_code.findall(html) ) ) )
    
    codes = [ c[2:] for c in codes ]
    return codes
